package com.ruoyi.erecord.locus.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.tools.enums.DeviceTypeEnum;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.erecord.locus.domain.bo.LocusBo;
import com.ruoyi.erecord.locus.domain.vo.LocusVo;
import com.ruoyi.erecord.locus.service.ILocusService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 轨迹
 *
 * @author cc
 * @date 2023-05-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/erecord/locus")
public class LocusController extends BaseController {

    private final ILocusService iLocusService;

    /**
     * 查询车辆轨迹列表
     */
    @SaCheckPermission("erecord:locus:list")
    @GetMapping("/list/car")
    public TableDataInfo<LocusVo> listCar(LocusBo bo, PageQuery pageQuery) {
        bo.setDeviceType(DeviceTypeEnum.CAR.getCode());
        return iLocusService.queryPageList(bo, pageQuery);
    }




    /**
     * 查询用户轨迹列表
     */
    @SaCheckPermission("erecord:locus:list")
    @GetMapping("/list/user")
    public TableDataInfo<LocusVo> listUser(LocusBo bo, PageQuery pageQuery) {
        bo.setDeviceType(DeviceTypeEnum.USER.getCode());
        return iLocusService.queryPageList(bo, pageQuery);
    }


    /**
     * 查询用户轨迹列表
     */
    @SaCheckPermission("erecord:locus:list")
    @GetMapping("/list/user/{uid}")
    public List<LocusVo> listUser(@PathVariable String uid) {
        LocusBo bo = new LocusBo();
        bo.setDeviceType(DeviceTypeEnum.USER.getCode());
        bo.setDeviceOriginalId(uid);
        return iLocusService.queryList(bo);
    }


    /**
     * 导出轨迹列表
     */
    @SaCheckPermission("erecord:locus:export")
    @Log(title = "轨迹", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(LocusBo bo, HttpServletResponse response) {
        List<LocusVo> list = iLocusService.queryList(bo);
        ExcelUtil.exportExcel(list, "轨迹", LocusVo.class, response);
    }

    /**
     * 获取轨迹详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("erecord:locus:query")
    @GetMapping("/{id}")
    public R<LocusVo> getInfo(@NotNull(message = "主键不能为空")
                              @PathVariable String id) {
        return R.ok(iLocusService.queryById(id));
    }

    /**
     * 新增轨迹
     */
    @SaCheckPermission("erecord:locus:add")
    @Log(title = "轨迹", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody LocusBo bo) {
        return toAjax(iLocusService.insertByBo(bo));
    }

    /**
     * 修改轨迹
     */
    @SaCheckPermission("erecord:locus:edit")
    @Log(title = "轨迹", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LocusBo bo) {
        return toAjax(iLocusService.updateByBo(bo));
    }

    /**
     * 删除轨迹
     *
     * @param ids 主键串
     */
    @SaCheckPermission("erecord:locus:remove")
    @Log(title = "轨迹", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iLocusService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
